﻿using System;
using System.Configuration;
using System.Data.SqlClient;

namespace wall
{
    public partial class PostMessage : System.Web.UI.Page
    {
        protected void btnPost_Click(object sender, EventArgs e)
        {
            string username = txtUsername.Text;
            string message = txtMessage.Text;

            string connectionString = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;

            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                string query = "INSERT INTO Messages (Username, Message, DatePosted) VALUES (@Username, @Message, @DatePosted)";
                SqlCommand command = new SqlCommand(query, connection);
                command.Parameters.AddWithValue("@Username", username);
                command.Parameters.AddWithValue("@Message", message);
                command.Parameters.AddWithValue("@DatePosted", DateTime.Now);

                try
                {
                    connection.Open();
                    command.ExecuteNonQuery();
                    lblMessage.Text = "留言发布成功！";
                    txtUsername.Text = "";
                    txtMessage.Text = "";
                }
                catch (Exception ex)
                {
                    lblMessage.Text = "发生错误：" + ex.Message;
                }
            }
        }
    }
}
